//
//  zhutiTableViewCell4.m
//  BAOASystem
//
//  Created by michael on 2018/5/23.
//  Copyright © 2018年 czd. All rights reserved.
//

#import "zhutiTableViewCell4.h"
#import "CollectionViewCell2.h"
@implementation zhutiTableViewCell4
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {

 _label = [[UILabel alloc]init];
    _label.frame = CGRectZero;
        _label.numberOfLines = 0;
      [_label setFont:[UIFont systemFontOfSize:16]];
       [self.contentView addSubview:_label];
  UIView *bomview1 = [[UIView alloc]initWithFrame:CGRectMake(0, 39, KScreenWidth, 1)];
bomview1.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];
    [self.contentView addSubview:bomview1];

    
//       self.collectionView.frame = CGRectMake(0, 40, KScreenWidth,((KScreenWidth-40-1)/2+60)*2+1);

 [self.contentView addSubview:self.collectionView];
      _bomview2 = [[UIView alloc]initWithFrame:CGRectZero];
_bomview2.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];
    [self.contentView addSubview:_bomview2];

}

return self;

}
-(UICollectionView *)collectionView{
if (!_collectionView) {
  UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((KScreenWidth-1)/2,((KScreenWidth-40-1)/2+60));

    layout.minimumLineSpacing = 1;
   layout.minimumInteritemSpacing = 1;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.pagingEnabled = true;
    _collectionView.bounces = NO;
//    _collectionView.scrollEnabled=NO;
    _collectionView.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];
//        _collectionView.backgroundColor = [UIColor whiteColor];

[_collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell1" bundle:nil] forCellWithReuseIdentifier:@"CollectionViewCell1"];
[_collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell2" bundle:nil] forCellWithReuseIdentifier:@"CollectionViewCell2"];
//self.collectionView.alwaysBounceVertical = YES;

    _collectionView.showsHorizontalScrollIndicator = false;
    
}
return _collectionView;

}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

 static NSString* cellId2 = @"CollectionViewCell2";
    CollectionViewCell2 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId2 forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
   NSArray *array = _dic[@"cppdlist"];
   cell.dic =array [indexPath.row];
   [cell layoutSubviews];
    return cell;
}
-(void)setDic:(NSDictionary *)dic{
[self.collectionView reloadData]; //重新换数据源的时候 记得重回用的cell上的
_dic = dic;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
   NSArray *array = _dic[@"cppdlist"];

if (array.count>4) {
        return 4;

}else{
    return array.count;

}

}
- (void)layoutSubviews{
[super layoutSubviews];
//  _view.frame = CGRectMake(5, 10, 10, 20);
//    _view.backgroundColor = [UIColor colorWithHexString:@"0aa0f6"];
     _label.frame = CGRectMake(10,10, KScreenWidth-20, 20);
    _label.text  = _dic[@"categoryName"];
//    _label.textColor = [UIColor colorWithHexString:@"484848"];
        _label.textColor = [UIColor colorWithHexString:@"0aa0f6"];
 NSArray *array = _dic[@"cppdlist"];

if (array.count>4) {
   _collectionView.frame =  CGRectMake(0, 40, KScreenWidth,((KScreenWidth-40-1)/2+60)*2+1);

}else if (array.count>0){
   _collectionView.frame =  CGRectMake(0, 40, KScreenWidth,((KScreenWidth-40-1)/2+60)*1+0);

}
_bomview2.frame = CGRectMake(0, _collectionView.bottom, KScreenWidth, 10);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
   NSArray *array = _dic[@"cppdlist"];
 NSDictionary *dic =array [indexPath.row];
shangpinxqViewController *vc = [[shangpinxqViewController alloc]init];
vc.pdid = dic[@"id"];
vc.hidesBottomBarWhenPushed = YES;
[self.ViewController.navigationController pushViewController:vc animated:YES];


}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
